<template>
    <view>
        <!-- #ifdef MP-WEIXIN -->
        <view v-if="isCanUse">
            <view>
                <view class='header'>
                    <image src='../../static/wx_login.png'></image>
                </view>
                <view class='content'>
					<view style="font-size: 36rpx;font-weight: bold;"> 智能小区 </view>
                    <view>申请获取以下权限</view>
                    <text>获得你的公开信息(昵称，头像、地区等)</text>
                </view>

                <button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">
                    授权登录
                </button>
            </view>
        </view>
        <!-- #endif -->
    </view>
</template>
<script>
	// const baseUrl = 'http://81.69.46.140:8080'
	const baseUrl = 'http://192.168.0.2:8080'
    export default {
        data() {
            return {
                SessionKey: '',
                OpenId: '',
                nickName: null,
                avatarUrl: null,
				jsCode:'',
                isCanUse: uni.getStorageSync('isCanUse')||true//默认为true
            };
        },
        methods: {
            wxGetUserInfo() {
                let _this = this;
                uni.getUserInfo({
                    provider: 'weixin',
                    success: function(infoRes) {
                        let nickName = infoRes.userInfo.nickName; //昵称
                        let avatarUrl = infoRes.userInfo.avatarUrl; //头像
                        try {
                            uni.setStorageSync('isCanUse', false);//记录是否第一次授权  false:表示不是第一次授权
                            _this.updateUserInfo(infoRes);
                        } catch (e) {}
                    },
                    fail(res) {}
                });
            },　　　　　　//登录
                login() {
                let _this = this;
                uni.showLoading({
                    title: '登录中...'
                });
    //          if(uni.getStorageSync('code')){
				// uni.reLaunch({
				//     url: '/pages/index/shopIndex'
				// });
			 // }
               // 1.wx获取登录用户code
                uni.login({
                    provider: 'weixin',
                    success: function(loginRes) {
                        let code = loginRes.code;
						_this.jsCode = code
						uni.setStorageSync('code',code)
						//2.将用户登录code传递到后台置换用户SessionKey、OpenId等信息
						uni.request({
						    url: baseUrl + '/login/onLogin',
						    data: {
						        code: code,
						    },
						    method: 'GET',
						    header: {
						        'content-type': 'application/json',
						    },
						    success: (res) => {
						        //openId、或SessionKdy存储//隐藏loading
						        uni.hideLoading();
								if (!_this.isCanUse) {
								    //非第一次授权获取用户信息
								    uni.getUserInfo({
								        provider: 'weixin',
								        success: function(infoRes) { 　　
								            let nickName = infoRes.userInfo.nickName; //昵称
								            let avatarUrl = infoRes.userInfo.avatarUrl; //头像
								                _this.updateUserInfo(infoRes);//调用更新信息方法
								        }
								    });
								}
						    }
						});
                       
						
                       
                    },
                });
            },
         //向后台更新信息
            updateUserInfo(userInfo) {
                let _this = this;
                uni.request({
                    url:baseUrl + '/login/saveUserInfo' ,
                    data: {
						iv:userInfo.iv,
						encryptedData:userInfo.encryptedData
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
						'jsCode':_this.jsCode
                    },
                    success: (res) => {
                        // if (res.data.state == "success") {
                            uni.reLaunch({//信息更新成功后跳转到小程序首页
                                url: '/pages/index/shopIndex'
                            });
                        // }
                    }
                   
                });
            }
        },
        onLoad() {//默认加载
            this.login();
        }
    }
</script>

<style>
    .header {
        margin: 90rpx 0 90rpx 50rpx;
        border-bottom: 1px solid #ccc;
        text-align: center;
        width: 650rpx;
        height: 300rpx;
        line-height: 450rpx;
    }

    .header image {
        width: 200rpx;
        height: 200rpx;
    }

    .content {
        margin-left: 50rpx;
        margin-bottom: 90rpx;
    }

    .content text {
        display: block;
        color: #9d9d9d;
        margin-top: 40rpx;
    }

    .bottom {
        border-radius: 80rpx;
        margin: 70rpx 50rpx;
        font-size: 35rpx;
    }
</style>
